#!/bin/busybox sh 
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>
#
#Copyright 2012 John Houwer 
#
if [[ -f "hashes" ]];then
	sha256sum -s -c "hashes" 

	if [[ "${?}" != "0" ]]; then
		echo \# CHECK FAILED! /boot is not valid
		bhashvaild="0";
	else 
		bhashvalid="1";
		echo \# CHECK SUCCESSFULL! /boot is valid;
	fi
else 
	echo file hashes not found
fi

MBRHASH=1
MBRCHASH=0
MBRHASH=$(dd if=/dev/sda bs=512 count=1 2>/dev/null  | sha256sum | cut -d' ' -f1)
if [[ -f "mbrhash" ]]; then 
	MBRCHASH="$(cat 'mbrhash')"
else 
	echo file mbrhash not found;
fi

if [[ "${MBRCHASH}" == "${MBRHASH}" ]]; then
	export mbrvalid=1;
	echo \# MBR is valid;
else
	echo \# MBR is not valid;
	export mbrvalid=0;
fi

for file in $(find /boot -type f); 
do 
	grep -q -E ^.{64}'  '"${file}"$ "hashes" || echo "${file}" is new. No hash could be found for this file.; 
done



if ! [[ -z "${1}" ]]; then
	if [[ "${1}" == "rehash" ]]; then
		echo \#
		echo \# THE NEW MBRHASH IS "${MBRHASH}"
		echo "${MBRHASH}" > "mbrhash"
		echo \#
		find /boot -type f -exec sha256sum {} \;  > "hashes"
	fi
fi

echo \#
echo \#
echo PRESS CTRL+ALT+DEL to reboot the System
